package com.zhuhanpeng.cloud.file.service.impl;

import com.zhuhanpeng.cloud.file.entity.COSEntity;
import com.zhuhanpeng.cloud.file.entity.PtFileInfoEntity;
import com.zhuhanpeng.cloud.file.entity.SearchEntity;
import com.zhuhanpeng.cloud.file.repository.FileDao;
import com.zhuhanpeng.cloud.file.service.FileService;
import com.zhuhanpeng.cloud.mybatis.entity.PtFileEntity;
import com.zhuhanpeng.cloud.mybatis.entity.PtSendShareEntity;
import com.zhuhanpeng.cloud.mybatis.entity.PtUserEntity;
import com.zhuhanpeng.cloud.mybatis.flexigrid.PadingRstType;
import com.zhuhanpeng.cloud.mybatis.flexigrid.PagingEntity;
import com.zhuhanpeng.cloud.spring.util.CloudUtil;
import com.zhuhanpeng.cloud.spring.util.UserFileUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@Service
public class FileServiceImpl implements FileService {
    @Value("${tencent.secret.id}")
    private String TENCENT_SECRET_ID;
    @Value("${tencent.secret.key}")
    private String TENCENT_SECRET_KEY;
    @Value("${tencent.bucket}")
    private String TENCENT_BUCKET;
    @Value("${tencent.region}")
    private String TENCENT_REGION;
    @Resource
    private FileDao filedao;

    @Override
    public PadingRstType<PtFileInfoEntity> queryFileInfoByPage(PtFileInfoEntity entity, PagingEntity paging) {
        paging.dealOrder(PtFileInfoEntity.class);
        PadingRstType<PtFileInfoEntity> padingRstType = new PadingRstType<>();
        padingRstType.setPage(paging.getPage());
        Integer total = filedao.getFileInfototal(entity);
        padingRstType.setTotal(total);
        List<PtFileInfoEntity> list = filedao.queryFileInfoByPage(entity, paging);
        padingRstType.setRawRecords(list);
        padingRstType.putItem();
        return padingRstType;
    }

    @Override
    public void uploadFileInfo(MultipartFile file) {
        //        生成uuid
        String Uuid = CloudUtil.generateUuid();
        String[] fileNameArray = file.getOriginalFilename().split("\\.");
        String fileType = fileNameArray[fileNameArray.length - 1];
        String fileName = Uuid + "." + fileType;
        long fileSize = file.getSize();
////        阿里云上传
//        COSEntity aliyunOSS = new COSEntity(ALIYUN_OSS_KEYID, ALIYUN_OSS_KEY_SECRET, ALIYUN_OSS_BUCKET_NAME, ALIYUN_OSS_ENDPOINT);
//        String url = UpdateUserFileUtil.aliyunUploadUserAvatar(aliyunOSS, fileName, file);
//        腾讯云上传
        COSEntity tencentCOS = new COSEntity(TENCENT_SECRET_ID, TENCENT_SECRET_KEY, TENCENT_BUCKET, TENCENT_REGION);
        String url = UserFileUtil.tencentUpdateFile(tencentCOS, "file" + "/" + fileName, file);
        PtFileEntity fileInfo = new PtFileEntity(Uuid, file.getOriginalFilename(), url, String.valueOf(fileSize));
        filedao.insertFileInfo(fileInfo);
    }


    @Override
    public void downloadFile(String fileName) {
        COSEntity tencentCOS = new COSEntity(TENCENT_SECRET_ID, TENCENT_SECRET_KEY, TENCENT_BUCKET, TENCENT_REGION);
        UserFileUtil.tencentDownloadFile(tencentCOS, "file" + "/" + fileName);
    }

    @Override
    public SearchEntity queryAllFileInfoList(SearchEntity entity) {
        entity.setPage();
        entity.setSearchInfoArray();
        List<PtFileEntity> list = filedao.queryAllFileInfoList(entity);
        Integer Total = filedao.countFileInfo(entity);
        entity.setTotal(Total);
        entity.setData(list);
        return entity;
    }

    @Override
    public void deleteFileInfo(PtFileEntity fileEntity) {
        filedao.deleteFile(fileEntity);
    }

    @Override
    public void collectionFileInfo(PtFileEntity fileEntity) {
        filedao.collectionFileInfo(fileEntity);
    }

    @Override
    public SearchEntity queryCollectionInfo(SearchEntity entity) {
        entity.setPage();
        entity.setSearchInfoArray();
        List<PtFileEntity> list = filedao.queryCollectionInfo(entity);
        Integer Total = filedao.countCollectionFileInfo(entity);
        entity.setTotal(Total);
        entity.setData(list);
        return entity;
    }


    @Override
    public void removeCollectionInfo(PtFileEntity fileEntity) {
        filedao.removeCollectionFileInfo(fileEntity);
    }

    @Override
    public SearchEntity queryRecycleFileInfo(SearchEntity entity) {
        entity.setPage();
        entity.setSearchInfoArray();
        List<PtFileEntity> list = filedao.queryRecycleFileInfo(entity);
        Integer Total = filedao.countRecycleFileInfo(entity);
        entity.setTotal(Total);
        entity.setData(list);
        return entity;
    }

    @Override
    public void refreshFileInfo(PtFileEntity entity) {
        filedao.refreshFileInfo(entity);
    }

    @Override
    public void deleteRecycleFileInfo(PtFileEntity fileEntity) {
        filedao.deleteRecycleFileInfo(fileEntity);
    }

    @Override
    public COSEntity getTencentCosInfo() {
        return new COSEntity(TENCENT_SECRET_ID, TENCENT_SECRET_KEY, TENCENT_BUCKET, TENCENT_REGION);
    }

    @Override
    public void shareFilesInfo(PtSendShareEntity entity) {

        filedao.shareFilesInfo(entity);
    }

    @Override
    public SearchEntity queryShareFileInfoList(SearchEntity entity) {
        entity.setPage();
        entity.setSearchInfoArray();
        List<PtSendShareEntity> list = filedao.queryShareFileInfoList(entity);
        Integer Total = filedao.countShareFileInfo(entity);
        entity.setTotal(Total);
        entity.setData(list);
        return entity;
    }

    @Override
    public void deleteShareFileInfos(PtSendShareEntity entity) {
        filedao.deleteShareFileInfo(entity);
    }

    @Override
    public PtFileEntity getFileInfo(PtSendShareEntity entity) {
        return filedao.getFileInfo(entity);
    }
}
